package com.baobao.web;

import com.baobao.dao.TeacherDAO;

import com.baobao.domain.Teacher;

import com.baobao.service.TeacherService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Teacher entities
 * 
 */

@Controller("TeacherController")
public class TeacherController {

	/**
	 * DAO injected by Spring that manages Teacher entities
	 * 
	 */
	@Autowired
	private TeacherDAO teacherDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Teacher entities
	 * 
	 */
	@Autowired
	private TeacherService teacherService;

	/**
	 * Save an existing Teacher entity
	 * 
	 */
	@RequestMapping("/saveTeacher")
	public String saveTeacher(@ModelAttribute Teacher teacher) {
		teacherService.saveTeacher(teacher);
		return "forward:/indexTeacher";
	}

	/**
	 * Edit an existing Teacher entity
	 * 
	 */
	@RequestMapping("/editTeacher")
	public ModelAndView editTeacher(@RequestParam String phoneKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("teacher", teacherDAO.findTeacherByPrimaryKey(phoneKey));
		mav.setViewName("teacher/editTeacher.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/teacherController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Select the Teacher entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteTeacher")
	public ModelAndView confirmDeleteTeacher(@RequestParam String phoneKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("teacher", teacherDAO.findTeacherByPrimaryKey(phoneKey));
		mav.setViewName("teacher/deleteTeacher.jsp");

		return mav;
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Create a new Teacher entity
	 * 
	 */
	@RequestMapping("/newTeacher")
	public ModelAndView newTeacher() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("teacher", new Teacher());
		mav.addObject("newFlag", true);
		mav.setViewName("teacher/editTeacher.jsp");

		return mav;
	}

	/**
	 * Select an existing Teacher entity
	 * 
	 */
	@RequestMapping("/selectTeacher")
	public ModelAndView selectTeacher(@RequestParam String phoneKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("teacher", teacherDAO.findTeacherByPrimaryKey(phoneKey));
		mav.setViewName("teacher/viewTeacher.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Teacher entities
	 * 
	 */
	public String indexTeacher() {
		return "redirect:/indexTeacher";
	}

	/**
	 * Show all Teacher entities
	 * 
	 */
	@RequestMapping("/indexTeacher")
	public ModelAndView listTeachers() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("teachers", teacherService.loadTeachers());

		mav.setViewName("teacher/listTeachers.jsp");

		return mav;
	}

	/**
	 * Delete an existing Teacher entity
	 * 
	 */
	@RequestMapping("/deleteTeacher")
	public String deleteTeacher(@RequestParam String phoneKey) {
		Teacher teacher = teacherDAO.findTeacherByPrimaryKey(phoneKey);
		teacherService.deleteTeacher(teacher);
		return "forward:/indexTeacher";
	}
}